package com.xintonghua.bussiness.ui.user.store;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xintonghua.base.widget.ConfirmDialog;
import com.xintonghua.base.widget.ShowAllListView;
import com.xintonghua.bussiness.BuildConfig;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.CartAdapter;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.CartBean;
import com.xintonghua.bussiness.bean.CartResponse;
import com.xintonghua.bussiness.event.CloseShopEventBus;
import com.xintonghua.bussiness.event.DeleteEventBus;
import com.xintonghua.bussiness.event.OrderEventBus;
import com.xintonghua.bussiness.ui.user.SettingActivity;
import com.xintonghua.bussiness.util.GlideCacheUtil;
import com.xintonghua.bussiness.util.MyState;
import com.xintonghua.bussiness.util.MyUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：wanxianyi
 * 时间：2018/3/31.
 * 包名：com.xintonghua.bussiness.ui.user.store
 */

public class CartActivity extends BaseActivity {
    @BindView(R.id.cb_all_check)
    CheckBox cbAllCheck;
    @BindView(R.id.tv_all_money)
    TextView tvAllMoney;
    @BindView(R.id.tv_cart_settle)
    TextView tvCartSettle;
    @BindView(R.id.lv_cart)
    ListView lvCart;
    private List<CartBean> list;
    CartAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initUI();
    }

    @Override
    public void initUI() {
        super.initUI();
        onSimpleActionBar();
        setSimpleActionBar("购物车");
        httpCent.shopList(this, MyState.requestOne);
        cbAllCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (list == null || list.size() == 0) {
                    return;
                }
                for (CartBean bean : list) {
                    bean.setCheck(b);
                }
                if (adapter != null) {
                    adapter.notifyDataSetChanged();
                    showMoney();
                }
            }
        });
    }

    @OnClick({R.id.tv_cart_settle})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_cart_settle:
                List<CartBean> senList = new ArrayList<>();
                for (CartBean bean : list) {
                    if (bean.getCount() == 0 || !bean.isCheck()) {
                        continue;
                    }
                    senList.add(bean);
                }
                if (senList.size() == 0) {
                    MyUtils.mToast(this, "请选择商品");
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putParcelableArrayList(BuildConfig.FLAVOR, (ArrayList<? extends Parcelable>) senList);
                openActivity(SureOrderActivity.class, bundle);
                break;
        }
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                CartResponse response = JSONObject.parseObject((String) data, CartResponse.class);
                list = response.getList();
                adapter = new CartAdapter(response.getList(), this);
                lvCart.setAdapter(adapter);
                break;
            case MyState.requestTwo:
                httpCent.shopList(this, MyState.requestOne);
                break;
            case MyState.requestThree:
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(OrderEventBus eventBus) {
        if (eventBus.getBean() != null) {
            httpCent.updateCar(eventBus.getBean().getId(), eventBus.getBean().getCount(), this, MyState.requestThree);
        }
        showMoney();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(final DeleteEventBus eventBus) {
        ConfirmDialog.showDialog(this, "提示", "是否确定删除订单？", "取消", "确定", null, new ConfirmDialog.OnRightListener() {
            @Override
            public void onClick(ConfirmDialog dialog) {
                httpCent.delShopping(eventBus.getOrderBean().getId(), CartActivity.this, MyState.requestTwo);
            }
        });

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(CloseShopEventBus closeShopEventBus) {
        finish();
    }

    /**
     * 展示合计
     */
    public void showMoney() {
        double money = 0.00;
        int count = 0;
        for (CartBean bean : list) {
            if (bean.getCount() == 0 || !bean.isCheck()) {
                continue;
            }
            count++;
            money = money + bean.getMoney() * bean.getCount();
        }
        Drawable drawable = getResources().getDrawable(count == list.size() ? R.drawable.cb_cart_check : R.drawable.cb_cart_uncheck);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        cbAllCheck.setCompoundDrawables(drawable, null, null, null);
        tvAllMoney.setText(MyUtils.getMoney(this, money));

    }

}
